﻿using Blog.Model;
using IBaseRepository;
using SqlSugar;
using SqlSugar.IOC;
using System.Linq.Expressions;

namespace BaseRepository
{
	public class BaseRepository<T> :SimpleClient<T>,IBaseRepository<T> where T : class,new()
	{
		public BaseRepository(ISqlSugarClient? context = null) : base(context)
		{
			base.Context = DbScoped.Sugar;
			////创建数据库
			//base.Context.DbMaintenance.CreateDatabase();
			//base.Context.CodeFirst.InitTables(
			//	typeof(Article),
			//	typeof(ArticleType),
			//	typeof(Author),
			//	typeof(Role),
			//	typeof(Comment)
			//	typeof(LikeRelation),
			//	typeof(DisLikeReation)
			//	typeof(CollectRelation)
			//);
		}

		public async Task<bool> CreateAsync(T tableType)
		{
			return await base.InsertAsync(tableType);
		}

		public async Task<bool> DeleteByIdAsync(int id)
		{
			return await base.DeleteByIdAsync(id);
		}

		public async Task<bool> EditAsync(T tableType)
		{
			return await base.UpdateAsync(tableType);
		}

		public virtual async Task<T> FindByIdAsync(int key)
		{
			return await base.GetByIdAsync(key);
		}

		public virtual async Task<List<T>> QueryAsync()
		{
			return await base.GetListAsync();
		}

		public virtual async Task<List<T>> QueryAsync(Expression<Func<T, bool>> func)
		{
			return await base.GetListAsync(func);
		}

		public virtual async Task<List<T>> QueryAsync(int PageIndex, int PageSize, RefAsync<int> Total)
		{
			return await base.Context.Queryable<T>().ToPageListAsync(PageIndex, PageSize, Total);
		}

		public virtual async Task<List<T>> QueryAsync(Expression<Func<T, bool>> func, int PageIndex, int PageSize, RefAsync<int> Total)
		{
			return await base.Context.Queryable<T>()
				.Where(func)
				.ToPageListAsync(PageIndex, PageSize, Total);
		}
	}
}
